//
//  DDLoginManager.m
//  DingDongZP
//
//  Created by 王威 on 16/3/15.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDLoginManager.h"
#import "DDLoginUserManager.h"
#import "DDNavigationController.h"
#import <RongIMKit/RongIMKit.h>
#import <objc/runtime.h>
#import "DDWelcomeViewController.h"
//#import "AppDelegate.h"
//#import <SVProgressHUD/SVProgressHUD.h>
#import "DDWelcomeViewController.h"

@interface DDLoginManager ()

@property (copy, nonatomic) DDLoginSuccessBlock loginSuccessBlock;
@property (copy, nonatomic) DDLoginFailureBlock loginFailureBlock;
@property (copy, nonatomic) DDLoginCancelBlock loginCancelBlock;

@property (strong, nonatomic) DDLoginViewController *loginViewController;

@end

@implementation DDLoginManager


+ (instancetype)sharedManager {
    
    static id sharedManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

//- (BOOL)isLogin {
//    
//    if ([[NSUserDefaults standardUserDefaults] objectForKey: @"kUserToken"]  {
//        
//        return true;
//    } else {
//        return  false;
//    }
//}

- (void)showLoginViewControllerFromViewController:(UIViewController *)fromViewController
                                 showCancelButton:(BOOL)showCancel
                                          success:(DDLoginSuccessBlock)success
                                          failure:(DDLoginFailureBlock)failure
                                           cancel:(DDLoginCancelBlock)cancel
{
    if (!fromViewController) {
        cancel();
        return;
    }
    
    self.loginSuccessBlock = success;
    self.loginFailureBlock = failure;
    self.loginCancelBlock = cancel;
    
    DDLoginViewController *viewController = [[DDLoginViewController alloc] initWithNibName:@"DDLoginViewController" bundle:nil];
    
    //只有压栈的时候才设置返回按钮
    if (fromViewController.navigationController) {
        
        [fromViewController.navigationController pushViewController:viewController animated:YES];
    } else {
        
        LDRootNavVC *loginNavigationController = [[LDRootNavVC alloc] initWithRootViewController:viewController];
        [fromViewController presentViewController:loginNavigationController animated:YES completion:nil];
    }
    
    self.loginViewController = viewController;
    
    WEAKSELF
    viewController.loginSuccessBlock = ^(void) {
        [weakSelf didLoginSuccess];
    };
    
}

- (void)showLoginViewControllerFromWindow:(UIWindow *)window
                         showCancelButton:(BOOL)showCancel
                                  success:(DDLoginSuccessBlock)success
                                  failure:(DDLoginFailureBlock)failure
                                   cancel:(DDLoginCancelBlock)cancel
{
    self.loginSuccessBlock = success;
    self.loginFailureBlock = failure;
    self.loginCancelBlock = cancel;
    
    
    DDLoginViewController *viewController = [[DDLoginViewController alloc] initWithNibName:@"DDLoginViewController" bundle:nil];
    self.loginViewController = viewController;
    
    WEAKSELF
    viewController.loginSuccessBlock = ^(void) {
        
        [weakSelf didLoginSuccess];
    };
    
    LDRootNavVC *loginNavigationController = [[LDRootNavVC alloc] initWithRootViewController:viewController];
    window.rootViewController = loginNavigationController;
}

- (BOOL)currentControllerIsLoginViewController:(UIWindow*)window
{
    
    if(!window || !self.loginViewController){
        return NO;
    }
    
    if(![window.rootViewController isKindOfClass:[DDNavigationController class]]){
        return NO;
    }
    
    DDNavigationController *navigationController = (DDNavigationController*)window.rootViewController;
    
    if(self.loginViewController == [navigationController.viewControllers objectAtIndex:0]){
        return YES;
    }
    
    return NO;
}

- (void)loginRongyun:(id (^)(NSString *userid))success failure:(id (^)(id error))failure {
    
    //统一处理
    [[LDUserInfoNet sharedInstance] queryToken:^(NSString * _Nonnull data) {
        
        
        if (data == nil) { return; };

        [[RCIM sharedRCIM] connectWithToken:data success:^(NSString *userId) {
            
            if (success != nil) {
                
                success(userId);
            }
            
        } error:^(RCConnectErrorCode status) {
            
            NSString *str = [NSString stringWithFormat:@"%ld",(long)status];
            
            if (failure != nil) {
                failure(str);
            }
            
        } tokenIncorrect:^{
            
            
            if (failure != nil) {
                failure(@"TOKEN错误");
            }
        }];
        
        
    } failure:^(NSString * _Nonnull data) {
        
        if (failure != nil) {
            failure(data);
        }
//        [[LDShowHUB shareInstance] showBarStatus:@"网络连接错误"];
    }];
    
}

- (void)loginOut
{
    
    [DDLoginUserManager clearLoginInfo];


    DDWelcomeViewController *vc = [[DDWelcomeViewController alloc] init];
    LDRootNavVC *rootVc = [[LDRootNavVC alloc] initWithRootViewController:vc];
    [UIApplication sharedApplication].keyWindow.rootViewController = rootVc;
    
}

#pragma mark - Private Method

- (void)didLoginSuccess {
    if (self.loginSuccessBlock) {
        self.loginSuccessBlock();
    }
}



@end
